package com.sgb.kjwl.viewmodel.self.logistics;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.self.BiddingOrderEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.base.model.entity.BaseEntity;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Time:2021/7/15
 * Author:黄淋
 * Description:竞价订单页面viewmodel
 */
public class BiddingOrderModel extends AndroidViewModel {
    public MutableLiveData<Integer> showLoading = new MutableLiveData<>();
    public MutableLiveData<List<BiddingOrderEntity>> ldOrderList = new MutableLiveData<>();
    private int pageNum = 1;

    public BiddingOrderModel(@NonNull Application application) {
        super(application);
    }


    /**
     * 获取竞价订单列表数据-货主
     */
    public void getBiddingOrderOwnerList(boolean isLoadMore, Map<String, Object> map) {
        showLoading.postValue(1);
        pageNum = isLoadMore ? pageNum + 1 : 1;
        NewAPIManager.getInstance().getLogisticsAPI().getBiddingOrderOwnerList(String.valueOf(pageNum), "20", (String) map.get("terminal"), (String) map.get("companyNo"),
                (String) map.get("teamNo"), (String) map.get("userNo")).enqueue(new Callback<BaseEntity<ListResponse<List<BiddingOrderEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<BiddingOrderEntity>>>> call, Response<BaseEntity<ListResponse<List<BiddingOrderEntity>>>> response) {
                showLoading.postValue(2);
                if (response.body() == null || response.body().getState() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ldOrderList.postValue(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<BiddingOrderEntity>>>> call, Throwable t) {
                showLoading.postValue(2);
            }
        });
    }

    /**
     * 获取竞价订单列表数据-承运方
     */
    public void getBiddingOrderCarriageList(boolean isLoadMore, Map<String, Object> map) {
        showLoading.postValue(1);
        pageNum = isLoadMore ? pageNum + 1 : 1;
        NewAPIManager.getInstance().getLogisticsAPI().getBiddingOrderCarriageList(String.valueOf(pageNum), "20", (String) map.get("terminal"), (String) map.get("companyNo"),
                (String) map.get("teamNo"), (String) map.get("userNo")).enqueue(new Callback<BaseEntity<ListResponse<List<BiddingOrderEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<BiddingOrderEntity>>>> call, Response<BaseEntity<ListResponse<List<BiddingOrderEntity>>>> response) {
                showLoading.postValue(2);
                if (response.body() == null || response.body().getState() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ldOrderList.postValue(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<BiddingOrderEntity>>>> call, Throwable t) {
                showLoading.postValue(2);
            }
        });
    }

}
